home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
198_01
/
file.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-21
|
23KB
|
769 lines
/* FILE.C: for MicroEMACS
The routines in this file handle the reading, writing
and lookup of disk files. All of details about the
reading and writing of the disk are in "fileio.c".
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
/*
* Read a file into the current
* buffer. This is really easy; all you do it
* find the name of the file, and call the standard
* "read a file into the current buffer" code.
* Bound to "C-X C-R".
*/
fileread(f, n)
{
register int s;
char fname[NFILEN];
if (restflag) /* don't allow this command if restricted */
return(resterr());
if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
return(s);
return(readin(fname, TRUE, FALSE));
}
/*
* Insert a file into the current
* buffer. This is really easy; all you do it
* find the name of the file, and call the standard
* "insert a file into the current buffer" code.
* Bound to "C-X C-I".
*/
insfile(f, n)
{
register int s;
char fname[NFILEN];
if (restflag) /* don't allow this command if restricted */
return(resterr());
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if ((s=mlreply("Insert file: ", fname, NFILEN)) != TRUE)
return(s);
return(ifile(fname));
}
/*
* Select a file for editing.
* Look around to see if you can find the
* fine in another buffer; if you can find it
* just switch to the buffer. If you cannot find
* the file, create a new buffer, read in the
* text, and switch to the new buffer.
* Bound to C-X C-F.
*/
filefind(f, n)
{
char fname[NFILEN]; /* file user wishes to find */
register int s; /* status return */
if (restflag) /* don't allow this command if restricted */
return(resterr());
if ((s=mlreply("Find file: ", fname, NFILEN)) != TRUE)
return(s);
return(getfile(fname, TRUE));
}
#if DECEDT
/*
* Select a file for editing and insert it into another window.
* If there is only one window, split it, otherwise pick a non-current window.
* Look around to see if you can find the
* file in another buffer; if you can find it
* just switch to the buffer. If you cannot find
* the file, create a new buffer, read in the
* text, and switch to the new buffer.
* Bound to C-X 4.
*/
filewfind(f, n)
{
char fname[NFILEN]; /* file user wishes to find */
register int s; /* status return */
register WINDOW *wp;
if (restflag) /* don't allow this command if restricted */
return(resterr());
if ((s=mlreply("Find file: ", fname, NFILEN)) != TRUE) return(s);
/* look for a window */
wp = wheadp;
while (wp != NULL && wp->w_wndp != curwp)
wp = wp->w_wndp;
if (wp == NULL) wp = curwp->w_wndp;
if (wp == NULL) {
if ((s=splitwind(f, n)) != TRUE) return(s);
wp = spltwp;
}
curwp = wp;
curbp = wp->w_bufp;
s = getfile(fname, TRUE);
return(s);
}
/*
* Read the last saved version of a file into the current buffer.
* Unound.
*/
filerevert(f, n)
{
char fname[NFILEN];
if (restflag) /* don't allow this command if restricted */
return(resterr());
if ((curbp->b_flag&BFCHG) == 0) /* Return, no changes. */
return (TRUE);
if (curbp->b_fname[0] == 0) { /* Must have a name. */
mlwrite("No file name");
return (FALSE);
}
strcpy(fname, curbp->b_fname);
return(readin(fname, TRUE, FALSE));
}
#endif
viewfile(f, n) /* visit a file in VIEW mode */
{
char fname[NFILEN]; /* file user wishes to find */
register int s; /* status return */
if (restflag) /* don't allow this command if restricted */
return(resterr());
if ((s=mlreply("View file: ", fname, NFILEN)) != TRUE)
return (s);
s = getfile(fname, FALSE);
if (s) { /* if we succeed, put it in view mode */
curwp->w_bufp->b_mode |= MDVIEW;
/* scan through and update mode lines of all windows */
upmode();
}
return(s);
}
#if CRYPT
resetkey() /* reset the encryption key if needed */
{
register int s; /* return status */
/* turn off the encryption flag */
cryptflag = FALSE;
/* if we are in crypt mode */
if (curbp->b_mode & MDCRYPT) {
if (curbp->b_key[0] == 0) {
s = setkey(FALSE, 0);
if (s != TRUE)
return(s);
}
/* let others know... */
cryptflag = TRUE;
/* and set up the key to be used! */
/* de-encrypt it */
crypt((char *)NULL, (unsigned) 0);
crypt(curbp->b_key, (unsigned) strlen(curbp->b_key));
/* re-encrypt it...seeding it to start */
crypt((char *)NULL, (unsigned) 0);
crypt(curbp->b_key, (unsigned) strlen(curbp->b_key));
}
return(TRUE);
}
#endif
getfile(fname, lockfl)
char fname[]; /* file name to find */
int lockfl; /* check the file for locks? */
{
register BUFFER *bp;
register LINE *lp;
register int i;
register int s;
char bname[NBUFN]; /* buffer name to put file */
#if MSDOS
mklower(fname); /* msdos isn't case sensitive */
#endif
for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
swbuffer(bp);
lp = curwp->w_dotp;
i = curwp->w_ntrows/2;
while (i-- && lback(lp)!=curbp->b_linep)
lp = lback(lp);
curwp->w_linep = lp;
curwp->w_flag |= WFMODE|WFHARD;
mlwrite("[Old buffer]");
return (TRUE);
}
}
makename(bname, fname); /* New buffer name. */
while ((bp=bfind(bname, FALSE, 0)) != NULL) {
/* old buffer name conflict code */
s = mlreply("Buffer name: ", bname, NBUFN);
if (s == ABORT) /* ^G to just quit */
return (s);
if (s == FALSE) { /* CR to clobber it */
makename(bname, fname);
break;
}
}
if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
mlwrite("Cannot create buffer");
return (FALSE);
}
if (--curbp->b_nwnd == 0) { /* Undisplay. */
curbp->b_dotp = curwp->w_dotp;
curbp->b_doto = curwp->w_doto;
curbp->b_markp = curwp->w_markp;
curbp->b_marko = curwp->w_marko;
curbp->b_fcol = curwp->w_fcol;
}
curbp = bp; /* Switch to it. */
curwp->w_bufp = bp;
curbp->b_nwnd++;
return(readin(fname, lockfl, FALSE)); /* Read it in. */
}
/*
Read file "fname" into the current buffer, blowing away any text
found there. Called by both the read and find commands. Return
the final status of the read. Also called by the mainline, to
read in a file specified on the command line as an argument.
The command bound to M-FNR is called after the buffer is set up
and before it is read.
*/
readin(fname, lockfl, recover)
char fname[]; /* name of file to read */
int lockfl; /* check for file locks? */
int recover; /* recover an autosaved file */
{
register LINE *lp1;
register LINE *lp2;
register int i;
register WINDOW *wp;
register BUFFER *bp;
register int s;
register int nbytes;
register int nline;
char mesg[NSTRING];
char tempname[NFILEN + 4];
#if FILOCK
if (lockfl && lockchk(fname) == ABORT)
return(ABORT);
#endif
bp = curbp; /* Cheap. */
if ((s=bclear(bp)) != TRUE) /* Might be old. */
return (s);
bp->b_flag &= ~(BFINVS|BFCHG);
strcpy(bp->